package com.zhihuiweijian.demo.controller;

import com.zhihuiweijian.demo.model.RolePermission;
import com.zhihuiweijian.demo.service.RolePermissionService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("role")
@Api(tags={"权限操作接口"})
public class RolePermissionController {

    @Autowired
    private RolePermissionService rolePermissionService;


    @GetMapping(value="/add")
    public  Map<String, Object> add(RolePermission rolePermission) {
        Map<String, Object> map = new HashMap();
        try {
            rolePermissionService.insert(rolePermission);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }


    @GetMapping(value="/delete")
    public Map<String, Object> delete(Long id) {

        Map<String, Object> map = new HashMap();
        try {
         rolePermissionService.delete(id);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }


    @GetMapping(value="/edit")
    public Map<String, Object> edit(RolePermission rolePermission) {
        Map<String, Object> map = new HashMap();
        try {
            rolePermissionService.update(rolePermission);
            map.put("state", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;

    }


    @GetMapping(value="/findAll")
    public Map<String, Object> findAll() {

        Map<String, Object> map = new HashMap();
        try {
            List<RolePermission> roles = rolePermissionService.findAll();
            map.put("state", true);
            map.put("data",roles);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }

    @GetMapping(value="/findAccess")
    public Map<String, Object> findA(String name) {
        Map<String, Object> map = new HashMap();
        try {
            List<RolePermission> users = rolePermissionService.selectByRole(name);
            map.put("state", true);
            map.put("data",users);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "添加失败!");
        }
        return map;
    }
}
